﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class DistrictForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CityEntity cityE = new CityEntity();
            CityService cityS = CityService.GetInstance();
            IList<CityEntity> lstCity = cityS.LoadAll();
            if (null != lstCity)
            {
                this.storeCity.DataSource = lstCity;
                this.storeCity.DataBind();
            }
        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string districtId)
        {
            DistrictEntity districtE = new DistrictEntity();
            DistrictService districtS = DistrictService.GetInstance();
            districtE.DistrictId = int.Parse(districtId);
            districtE = districtS.LoadByPrimaryKey(districtE);
            if (null == districtE)
                return;
            hdfDistrictId.Value = districtE.DistrictId;
            txtDistrictName.Text = districtE.DistrictName;
            cbbCity.SetValue(districtE.CityId);
            txtDescription.Text = districtE.Description;
            ckbIsActive.Checked = districtE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            DistrictEntity districtE = new DistrictEntity();
            DistrictService districtS = DistrictService.GetInstance();
            if ("" != hdfDistrictId.Value.ToString())
            {
                string districtId = hdfDistrictId.Value.ToString();
                districtE.DistrictId = int.Parse(districtId);
                districtE = districtS.LoadByPrimaryKey(districtE);
                districtE.DistrictName = txtDistrictName.Text;                
                districtE.CityId = int.Parse(cbbCity.SelectedItem.Value);
                districtE.Description = txtDescription.Text;
                districtE.IsActive = ckbIsActive.Checked;
                districtE.UpdatedDate = DateTime.Now;
                districtE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                districtS.Update(districtE);
            }
            else
            {
                districtE.DistrictName = txtDistrictName.Text;
                districtE.CityId = int.Parse(cbbCity.SelectedItem.Value);
                districtE.Description = txtDescription.Text;
                districtE.IsActive = ckbIsActive.Checked;
                districtE.CreatedDate = DateTime.Now;
                districtE.CreatedBy = FSSContext.Current.User.EmployeeId;
                districtE.UpdatedDate = DateTime.Now;
                districtE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                districtS.Insert(districtE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}